<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Share extends REST_Controller
{

    protected $rules = array(

    );

    private $rdata = array();

    function __construct()
    {

        parent::__construct();
        $this->returnInfo = lang('returnInfo');
        $this->rdata['returnNo'] = '400';
        $this->rdata['returnInfo'] = '请求操作失败';
        $this->rdata['returnTime'] = time();
        $this->rdata['Secure'] = JSON_SECURE;
        $this->rdata['Content2'] = null;
        $this->rdata['Content'] = null;
        $this->rdata['returnData'] = null;
        $this->load->helpers('uisite_helper');
    }
    /*
    * 获取消息中心的系统消息和推广消息的详情信息
    */
    public function getMessageInfo_get(){
        session_start();
        $this->load->model('common_model','common','default');
        $this->load->model('user_model','user','default');
        $UID   = $this->session->userdata('UAID');
        $token = $this->em_getallheaders('AUTHORIZATION');//上次登录后产生的令牌
        $data=$this->get();
        $UID=$this->checkUserToken($token,$UID);
        if($UID){
            $count=$this->user->countLog(array('id'=>$UID,'phone_valid'=>1,'is_delect'=>0));
            if($count==0){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '该用户不存在';
                $this->response($this->rdata);
            }
            $userid=$UID;
        }else{
            $share_id=isset($data['ShareId'])?$data['ShareId']:'';
            if(!$share_id){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '您的登录已经失效，请重新登录';
                $this->response($this->rdata);
            }else{
                $res=$this->user->onceInfo(array('share_id'=>$share_id,'phone_valid'=>1),'id');
                if($res){
                    $userid=$res['id'];
                }else{
                    $this->rdata['ReturnCode']   = '400';
                    $this->rdata['ReturnInfo'] = '您的登录已经失效，请重新登录';
                    $this->response($this->rdata);
                }
            }
        }
        if($data['Type']==1){
            $count=$this->common->countLog(array('cid'=>6,'audit'=>1,'id'=>$data['Id']),'message');
            if($count==0){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '内容不存在';
                $this->response($this->rdata);
            }
            $res=$this->common->onceInfo(array('cid'=>6,'audit'=>1,'id'=>$data['Id']),'id,title,content,timeline','message');
            $res['timeline']=date('Y-m-d H:i:s',$res['timeline']);
            $vdata['info']=$res;
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '信息获取成功';
            $this->rdata['Content2'] = $vdata;
            $this->rdata['Content'] = $vdata;
            $this->response($this->rdata);
        }elseif($data['Type']==2){
            $count=$this->common->countLog(array('cid'=>7,'audit'=>1,'id'=>$data['Id']),'message');
            if($count==0){
                $this->rdata['ReturnCode']   = '400';
                $this->rdata['ReturnInfo'] = '内容不存在';
                $this->response($this->rdata);
            }
            $res=$this->common->onceInfo(array('cid'=>7,'audit'=>1,'id'=>$data['Id']),'id,title,content,timeline','message');
            $res['timeline']=date('Y-m-d H:i:s',$res['timeline']);
            $vdata['info']=$res;
            $this->rdata['ReturnCode']   = '200';
            $this->rdata['ReturnInfo'] = '信息获取成功';
            $this->rdata['Content2'] = $vdata;
            $this->rdata['Content'] = $vdata;
            $this->response($this->rdata);
        }else{
            $this->rdata['ReturnCode']   = '400';
            $this->rdata['ReturnInfo'] = '请求类型错误';
            $this->response($this->rdata);
        }
    }
    private  function checkUserToken($token,$UID){
        if (! $token && ! $UID) {
            return false;
        }
        if ($token_info = json_decode($this->aes->decrypt($token,TokenKEY), 1)) {
            $UID = isset($token_info['id']) ? $token_info['id'] : 0;
        }
        if(!$token_info){
            return false;
        }
        return $UID;
    }
    private  function checkToken($token,$UID){
        if (! $token && ! $UID) {
            $this->rdata['ReturnCode']   = '0014';
            $this->rdata['ReturnInfo'] = '您的登录已经失效，请重新登录';
            $this->response($this->rdata);
        }
        if ($token_info = json_decode($this->aes->decrypt($token,TokenKEY), 1)) {
            $UID = isset($token_info['id']) ? $token_info['id'] : 0;
        }
        if(!$token_info){
            $this->rdata['ReturnCode']   = '0011';
            $this->rdata['ReturnInfo'] = 'TOKEN 验证失败';
            $this->response($this->rdata);
        }
        return $UID;
    }
    private function em_getallheaders($key=false) {

        foreach ($_SERVER as $name => $value) {

            if (substr($name, 0, 5) == 'HTTP_') {

                $headers[str_replace(' ', '-', ucwords(strtoupper(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        if ($key && isset($headers[$key])) {

            return $headers[$key];
        }
        return false;
    }
}